home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1988 / 06 / listing.4 < prev    next >
Text File  |  1988-08-29  |  2KB  |  53 lines

  1.   1  /*
  2.   2   * client.c         -----   A potential customer for the background process
  3.   3   *                          (i.e., "daemon").  All it does is send a string
  4.   4   *                          and its process id to the daemon.
  5.   5   *                          Note that read() and write() are atomic, 
  6.   6   *                          uninterruptable system calls.  This guarantees
  7.   7   *                          that our data arrive in one piece and not
  8.   8   *                          interleaved with some other client's data.
  9.   9   */
  10.  10  
  11.  11  #include <stdio.h>
  12.  12  #include <fcntl.h>
  13.  13  #include <sys/signal.h>
  14.  14  #include "local.h"
  15.  15  
  16.  16  int fd;     /* File descriptor for the Named Pipe (FIFO) */
  17.  17  PACKET msg; /* Information to be sent over a FIFO to a server process */
  18.  18  
  19.  19  main(argc,argv)
  20.  20  char **argv;
  21.  21  {
  22.  22      int quit();                         /* What to do when killed */
  23.  23  
  24.  24      srand(getpid());
  25.  25  
  26.  26      msg.pid = getpid();                 /* Setup the data to be sent */
  27.  27      strcpy(msg.string, "Message from a Client");
  28.  28  
  29.  29  
  30.  30  /* Open the FIFO (Named Pipe) for writing */
  31.  31  
  32.  32      if ( (fd = open(PIPE, O_WRONLY))  ==  -1 )
  33.  33          fprintf(stderr, "Cannot open PIPE in client\n"), exit(1);
  34.  34  
  35.  35      signal(SIGINT, quit);               /* Prepare for a Break */
  36.  36  
  37.  37      while(1)
  38.  38      {
  39.  39          write(fd, &msg, sizeof msg);    /* Write to server */
  40.  40  
  41.  41          sleep(rand() % 10);             /* Nice and slow, so we can see msgs */
  42.  42      }
  43.  43  }
  44.  44  
  45.  45  /*****************************************************************************/
  46.  46  
  47.  47  quit()
  48.  48  {
  49.  49      strcpy(msg.string, "Done");         /* Tell server to kill itself */
  50.  50      write(fd, &msg, sizeof msg);
  51.  51      exit(0);                            /* Kill this particular client */
  52.  52  }
  53.